package org.apache.lucene.demo;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

public class TestHttpConnection {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String uri = "http://www.baidu.com";
		HttpClient httpClient = new HttpClient();
		//设置超时时间5秒
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
		GetMethod getMethod = new GetMethod(uri);
		getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
		
		try {
			int statusCode = httpClient.executeMethod(getMethod);
			if(statusCode != HttpStatus.SC_OK){
				System.out.println("Method failed !"+getMethod.getStatusLine());
			}
			Header[] headers = getMethod.getResponseHeaders();
			for(Header header: headers){
				System.out.println(header.getName() +","+ header.getValue());
			}
			byte[] responseBodys = getMethod.getResponseBody();
			System.out.println("header response = "+ new String(responseBodys));
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			getMethod.releaseConnection();
		}
		
		extractKeyWordText(uri, "百度");
	}
	
	
	public static void extractKeyWordText(String url,String keyWord){
		try {
			//解析器对象,url为参数
			Parser parser = new Parser(url);
			parser.setEncoding("gb2312");
			//迭代所有节点, null 表示不使用 NodeFilter
			NodeList list = parser.parse(null);
			//从初始的节点列表跌倒所有的节点
			processNodeList(list, keyWord);
		} catch (ParserException e) {
			e.printStackTrace();
		}
	}
	
	private static void processNodeList(NodeList list,String keyWord){
		SimpleNodeIterator iterator = list.elements();
		while(iterator.hasMoreNodes()){
			Node node = iterator.nextNode();
			//该节点的子节点
			NodeList childList = node.getChildren();
			//为空，说明为值节点
			if (childList == null) {
				String result = node.toPlainTextString();
				if(result.indexOf(keyWord) != -1){
					System.out.println(result);
				}
			}else{
				//孩子节点不为空，继续迭代该孩子节点
				processNodeList(childList, keyWord);
			}
		}
	}

}
